#include "bd_structs.h"
#include "utils/network/json_analysis.h"
#include "utils/str_util.h"
#include <string.h>

// ✅http请求json数据解析处理
#include <stdio.h>
#include <stdbool.h>

/**
 * http请求响应数据json解析赋值结构体变量
 * @param DeviceAuthResponse *g_device_auth 响应数据协议对应的结构体，传指针参数，直接修改字段会直接作用在原变量上
 * @param cJSON *json 响应数据json
 * @return void
 */
void config_storage_info_rep(StorageInfo *storage_info, cJSON *json) {
    if (!json) {
        printf("❌ JSON解析失败\n");
        return;
    }

    cJSON *item;
    long long total = 0, free_space = 0, used = 0, request_id = 0;
    bool expire = false;

    item = cJSON_GetObjectItem(json, "total");
    if (cJSON_IsNumber(item)) total = item->valuedouble;

    item = cJSON_GetObjectItem(json, "free");
    if (cJSON_IsNumber(item)) free_space = item->valuedouble;

    item = cJSON_GetObjectItem(json, "used");
    if (cJSON_IsNumber(item)) used = item->valuedouble;

    item = cJSON_GetObjectItem(json, "request_id");
    if (cJSON_IsNumber(item)) request_id = item->valuedouble;

    item = cJSON_GetObjectItem(json, "expire");
    if (cJSON_IsBool(item)) expire = cJSON_IsTrue(item);

    // 转换为GB（1 GB = 1024^3 = 1073741824 字节）
    storage_info->total_GB = total / (1024.0 * 1024.0 * 1024.0);
    storage_info->free_GB  = free_space / (1024.0 * 1024.0 * 1024.0);
    storage_info->used_GB  = used / (1024.0 * 1024.0 * 1024.0);
    storage_info->expire  = expire;
    storage_info->request_id  = request_id;

    printf("=== Storage Info Struct ===\n");
    printf("Total space size: %.2f GB\n", storage_info->total_GB);
    printf("Used size: %.2f GB\n", storage_info->used_GB);
    printf("Free size:   %.2f GB\n", storage_info->free_GB);
    printf("expire: %s\n", storage_info->expire ? "yes" : "no");
    printf("request ID: %lld\n", storage_info->request_id);
}


/**
 * http请求响应数据json解析赋值结构体变量
 * @param DeviceAuthResponse *g_device_auth 响应数据协议对应的结构体，传指针参数，直接修改字段会直接作用在原变量上
 * @param cJSON *json 响应数据json
 * @return void
 */
void config_device_auth_rep(DeviceAuthResponse *g_device_auth, cJSON *json) {
    char *verification_url = getCJSONString(json, "verification_url");
    strcpy(g_device_auth->verification_url, verification_url);
    char *device_code = getCJSONString(json, "device_code");
    strcpy(g_device_auth->device_code, device_code);
    char *user_code = getCJSONString(json, "user_code");
    strcpy(g_device_auth->user_code, user_code);
    char *qrcode_url = getCJSONString(json, "qrcode_url");
    // strcpy(g_device_auth.qrcode_url, qrcode_url);
    strncpy(g_device_auth->qrcode_url, qrcode_url, sizeof(qrcode_url) - 1);//防止越界的赋值语句
    int expires_in = getCJSONInt(json, "expires_in");
    g_device_auth->expires_in = expires_in;
    int interval = getCJSONInt(json, "interval");
    g_device_auth->interval = interval;
}

/**
 * http请求响应数据json解析赋值结构体变量
 * @param OAuthToken *g_oauth_token 响应数据协议对应的结构体，传指针参数，直接修改字段会直接作用在原变量上
 * @param cJSON *json 响应数据json
 * @return void
 */
void config_access_token_rsp(OAuthToken *g_oauth_token, cJSON *json) {
    char *access_token = getCJSONString(json, "access_token");
    if (is_empty_str(access_token))
    {
        return;
    }

    strcpy(g_oauth_token->access_token, access_token);
    char *refresh_token = getCJSONString(json, "refresh_token");
    strcpy(g_oauth_token->refresh_token, refresh_token);
    int expires_in = getCJSONInt(json, "expires_in");
    g_oauth_token->expires_in = expires_in;
    char *scope = getCJSONString(json, "scope");
    strcpy(g_oauth_token->scope, scope);
}

/**
 * http请求响应数据json解析赋值结构体变量
 * @param UserInfo *user_info 响应数据协议对应的结构体，传指针参数，直接修改字段会直接作用在原变量上
 * @param cJSON *json 响应数据json
 * @return void
 */
void config_info_rsp(UserInfo *user_info, cJSON *json) {

    char *openid = getCJSONString(json, "openid");
    if (is_empty_str(openid))
    {
        return;
    }

    strcpy(user_info->openid, openid);
    char *portrait = getCJSONString(json, "portrait");
    strcpy(user_info->portrait, portrait);
    char *username = getCJSONString(json, "username");
    strcpy(user_info->username, username);
    char *birthday = getCJSONString(json, "birthday");
    strcpy(user_info->birthday, birthday);
    char *sex = getCJSONString(json, "sex");
    strcpy(user_info->sex, sex);
    char *is_bind_mobile = getCJSONString(json, "is_bind_mobile");
    strcpy(user_info->is_bind_mobile, is_bind_mobile);
    char *is_realname = getCJSONString(json, "is_realname");
    strcpy(user_info->is_realname, is_realname);

}
